<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Import\ExpertImport;
use App\Expert;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class ExpertController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '专家库';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Expert());

        $grid->model()->where('role_type', 3);

        $grid->column('username', __('用户名'));
        $grid->column('name', __('姓名'));
        $grid->column('company', __('单位'));
        $grid->column('academic_title', __('职称'));
        $grid->column('id_card', __('身份证'));
        $grid->column('major', __('专业领域'));
        $grid->column('mobile', __('电话'));


        $grid->column('bank_account', __('银行账号'))->copyable()->display(function ($value) {
            return $this->bank_account ? $value : '';
        });
        $grid->column('bank', __('开户银行'));


        $states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $grid->column('enabled', __('启用'))->switch($states);
        $grid->column('created_at', __('Created at'))->hide();
        //$grid->column('updated_at', __('Updated at'));
        $grid->tools(function(Grid\Tools $tools){
            $tools->append(new ExpertImport());
        });
        $grid->disableFilter();
        $grid->quickSearch(function ($query, $value) {
            $query->where(function ($query) use ($value) {
                $query->where('username', 'like', "%$value%")
                    ->orWhere('name', 'like', "%$value%")
                    ->orWhere('mobile', 'like', "%$value%")
                    ->orWhere('major', 'like', "%$value%")
                    ->orWhere('id_card', 'like', "%$value%")
                    ->orWhere('company', 'like', "%$value%")
                    ->orWhere('bank', 'like', "%$value%")
                    ->orWhere('bank_account', 'like', "%$value%");
            });
        })->placeholder('关键字');
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Expert());

        $form->text('username', __('用户名'))->required()
            ->creationRules(['required', "unique:evaluation_users"], ['required' => '不能为空', 'unique' => '用户名重复'])
            ->updateRules(['required', "unique:evaluation_users,username,{{id}}"], ['required' => '不能为空', 'unique' => '用户名重复']);
        $form->password('password', trans('admin.password'))->rules('confirmed|required');
        $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
            ->default(function ($form) {
                return $form->model()->password;
            });
        $form->text('name', __('姓名'))->required();
        $form->text('company', __('单位'));
        $form->text('academic_title', __('职称'));
        $form->text('id_card', __('身份证'));
        $form->text('major', '专业领域');
        $form->mobile('mobile', __('电话'));
        $form->text('bank_account', __('银行账号'));
        $form->text('bank', __('开户银行'));

        $states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('enabled', '启用')->states($states)->default(1);
        $form->hidden('role_type')->default(3);
        $form->ignore(['password_confirmation']);

        $form->saving(function (Form $form) {
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
            }
        });
        return $form;
    }
}
